home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
util
/
callbacks.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
10KB
|
292 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from __future__ import with_statement
from introspect import funcinfo
from primitives import Delegate
from functools import wraps
from logging import getLogger
from traceback import print_exc
import sys
import threading
log = getLogger('callbacks')
CALLBACKS = ('success', 'error', 'timeout')
call_later_lock = threading.Lock()
call_laters = dict()
def register_call_later(threadname, call_later):
call_later_lock.__enter__()
try:
call_laters[threadname] = call_later
finally:
pass
def unregister_call_later(threadname):
call_later_lock.__enter__()
try:
call_laters.pop(threadname)
finally:
pass
DO_NOTHING = lambda *a, **k: pass
class CallLater(object):
def __init__(self, cb, threadname = None):
if not threadname:
pass
self.threadname = threading.currentThread().getName()
if self.threadname not in call_laters:
try:
cb_dbg_str = ', '.join((lambda .0: for s in .0:
str(s))([
funcinfo(cb),
cb.func_code.co_filename,
cb.func_code.co_firstlineno]))
except Exception:
cb_dbg_str = 'Error making debug string. Repr is: {%s}' % funcinfo(cb)
if cb is not DO_NOTHING:
pass
self.cb = cb
def __repr__(self):
return '<%s %s>' % (type(self).__name__, funcinfo(self.cb))
def __call__(self, *a, **k):
if threading.currentThread().getName() != self.threadname and self.threadname in call_laters:
try:
return (None, None, call_laters[self.threadname])((lambda : self.cb(*a, **k)))
except Exception:
print >>sys.stderr, 'callback is %s' % funcinfo(self.cb)
raise
except:
None<EXCEPTION MATCH>Exception
None<EXCEPTION MATCH>Exception
try:
return self.cb(*a, **k)
except Exception:
import sys as sys
print >>sys.stderr, '%s in %s (%s). args/kwargs are: %r,%r' % (getattr(self.cb, '__name__', funcinfo(self.cb)), self.cb.__module__, funcinfo(self.cb), a, k)
raise
class CallLaterDelegate(Delegate):
def __init__(self, *a):
Delegate.__init__(self, (lambda .0: for x in .0:
CallLater(x))(a))
def __iadd__(self, f):
if isinstance(f, list):
for thing in f:
self += thing
else:
self.append(CallLater(f))
return self
class Callback(object):
normal_callback_names = CALLBACKS
def __init__(self, **cbs):
for name, callback in cbs.iteritems():
if not callable(callback):
raise TypeError('keyword args to Callback must be callable: %s' % name)
setattr(self, name, CallLaterDelegate(callback))
attr = getattr(self, name)
def __getattr__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError:
if attr in self.normal_callback_names:
return CallLaterDelegate()
else:
raise
except:
attr in self.normal_callback_names
def __setattr__(self, attr, val):
object.__setattr__(self, attr, val)
def __call__(self, *a, **k):
return self.success(*a, **k)
def __repr__(self):
return '<%s %s>' % (type(self).__name__, ' '.join((lambda .0: for item in .0:
'%s=%s' % item)(self.__dict__.items())))
EMPTY_CALLBACK = Callback()
DefaultCallback = EMPTY_CALLBACK
CALLBACK_ATTR = '_iscallback'
def callsback(func):
def wrapper(*secret_a, **secret_kws):
cb = None
if 'callback' in secret_kws:
if (any,)((lambda .0: for cbname in .0:
cbname in secret_kws)(CALLBACKS)):
raise AssertionError('use callback or individual callbacks')
cb = secret_kws['callback']
else:
for cbname in CALLBACKS:
if cbname in secret_kws:
if cb is None:
cb = Callback()
if not callable(secret_kws[cbname]):
raise TypeError('%s must be callable' % cbname)
mycb = CallLaterDelegate(secret_kws[cbname])
setattr(cb, cbname, mycb)
del secret_kws[cbname]
continue
secret_kws['callback'] = cb
if cb is None:
secret_kws['callback'] = cb = Callback()
try:
val = func(*secret_a, **secret_kws)
except Exception:
e = None
print_exc()
callany = callany
import util
if not callany(cb.error, e):
raise
val = None
if val is True:
cb.success()
return val
wrapper = (wraps(func),)(wrapper)
setattr(wrapper, CALLBACK_ATTR, True)
return wrapper
def is_callsback(f):
return bool(getattr(f, CALLBACK_ATTR, False))
def do_cb(seq, callback = None):
CallbackSequence(seq, callback = callback)()
do_cb = callsback(do_cb)
class CallbackSequence(object):
def __init__(self, sequence, callback):
self.iter = iter(sequence)
self.callback = callback
self.n = 0
def __call__(self, *a):
self.n = self.n + 1
try:
next = self.iter.next()
except StopIteration:
log.debug('stop iteration, calling success: %r', funcinfo(self.callback.success))
self.callback.success()
log.debug('%s #%d: %r', funcinfo(self.callback), self.n, funcinfo(next))
next(*a, **dict(success = self, error = self.callback.error))
def do_cb_na(seq, callback = None):
CallbackSequenceNoArgs(seq, callback = callback)()
do_cb_na = callsback(do_cb_na)
class CallbackSequenceNoArgs(object):
def __init__(self, sequence, callback):
self.iter = iter(sequence)
self.callback = callback
self.n = 0
def __call__(self, *a):
self.n = self.n + 1
try:
next = self.iter.next()
except StopIteration:
log.debug('stop iteration, calling success: %r', funcinfo(self.callback.success))
self.callback.success()
log.debug('%s #%d: %r', funcinfo(self.callback), self.n, funcinfo(next))
next(**dict(success = self, error = self.callback.error))
def wxcall(func):
def wrapper(*a, **k):
CallLater(func, threadname = 'MainThread')(*a, **k)
wrapper = (wraps(func),)(wrapper)
return wrapper
if __name__ == '__main__':
class MyProtocol(object):
def networkOperation(self, someArg, callback = None):
callback.success()
networkOperation = callsback(networkOperation)
def good():
print 'success'
def bad():
print 'bad'
c = Callback(success = good)
MyProtocol().networkOperation(5, callback = c)
MyProtocol().networkOperation(5, success = good)
MyProtocol().networkOperation(5)